﻿using System;
using System.IO.IsolatedStorage;

namespace Moonwish.Main.Silverlight
{
    /// <summary>
    /// 存储空间管理,write by zhlin 2012-03-20
    /// </summary>
    public class StrogeSpaceManager
    {
        /// <summary>
        /// 申请独立存储空间，单位MB。
        /// </summary>
        /// <param name="size"></param>
        /// <returns></returns>
        public static bool ApplyStrogeSpace(double size)
        {
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    Int64 quotSize = Convert.ToInt64(size * 1024 * 1024);
                    if (store.Quota < quotSize)
                    {
                        return store.IncreaseQuotaTo(quotSize);
                    }
                    return true;
                }
            }
            catch (IsolatedStorageException)
            {
                return false;
            }
        }

        /// <summary>
        /// 检查是否需要申请空间。
        /// </summary>
        /// <param name="requiredSize"></param>
        /// <returns></returns>
        public static bool CheckRequiredApplySpace(double requiredSize)
        {
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    Int64 quotSize = Convert.ToInt64(requiredSize * 1024 * 1024);
                    if (store.Quota < quotSize)
                    {
                        return true;
                    }
                    return false;
                }
            }
            catch (IsolatedStorageException)
            {
                return false;
            }
        }
    }
}
